{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-07-30T06:46:01.747941Z",
     "start_time": "2021-07-30T06:45:59.007818Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  - url: assets/images/album/origin/IMG_3673.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3673.JPG\n",
      "  - url: assets/images/album/origin/IMG_3671.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3671.JPG\n",
      "  - url: assets/images/album/origin/IMG_3663.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3663.JPG\n",
      "  - url: assets/images/album/origin/IMG_3387.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3387.JPG\n",
      "  - url: assets/images/album/origin/IMG_3365.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3365.JPG\n",
      "  - url: assets/images/album/origin/IMG_3184.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3184.JPG\n",
      "  - url: assets/images/album/origin/IMG_3183.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3183.JPG\n",
      "  - url: assets/images/album/origin/IMG_3181.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3181.JPG\n",
      "  - url: assets/images/album/origin/IMG_3100.JPG\n",
      "    image_path: assets/images/album/ths/IMG_3100.JPG\n",
      "  - url: assets/images/album/origin/IMG_4023.jpg\n",
      "    image_path: assets/images/album/ths/IMG_4023.jpg\n",
      "  - url: assets/images/album/origin/IMG_4017 2.jpg\n",
      "    image_path: assets/images/album/ths/IMG_4017 2.jpg\n",
      "  - url: assets/images/album/origin/IMG_4017.jpg\n",
      "    image_path: assets/images/album/ths/IMG_4017.jpg\n",
      "  - url: assets/images/album/origin/IMG_3140.jpg\n",
      "    image_path: assets/images/album/ths/IMG_3140.jpg\n",
      "  - url: assets/images/album/origin/IMG_3137.jpg\n",
      "    image_path: assets/images/album/ths/IMG_3137.jpg\n",
      "  - url: assets/images/album/origin/IMG_2900.jpg\n",
      "    image_path: assets/images/album/ths/IMG_2900.jpg\n",
      "  - url: assets/images/album/origin/IMG_2885.jpg\n",
      "    image_path: assets/images/album/ths/IMG_2885.jpg\n",
      "  - url: assets/images/album/origin/IMG_2811.jpg\n",
      "    image_path: assets/images/album/ths/IMG_2811.jpg\n",
      "  - url: assets/images/album/origin/IMG_3136.jpg\n",
      "    image_path: assets/images/album/ths/IMG_3136.jpg\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "origin_pics =  os.listdir('assets/images/album/origin/')\n",
    "th_pics =  os.listdir('assets/images/album/ths/')\n",
    "file = origin_pics[0]\n",
    "tdir = 'assets/images/album/'\n",
    "def cropimg(file):\n",
    "    from PIL import Image\n",
    "    im = Image.open(tdir+'origin/'+file)\n",
    "    width,height = im.size\n",
    "    im = im.resize((int(width*0.3),int(height*0.3)))\n",
    "    width,height = im.size\n",
    "    a = int(min(width,(height/3*4))/4)\n",
    "    start1 = (width-a*4)/2\n",
    "    end1 = start1+a*4\n",
    "    start2 = (height-a*3)/2\n",
    "    end2 = start2+a*3\n",
    "    im = im.crop((int(start1),int(start2),int(end1),int(end2)))\n",
    "    im.save(tdir+'ths/'+file)\n",
    "    print('  - url: '+tdir+'origin/'+file)\n",
    "    print('    image_path: '+tdir+'ths/'+file)\n",
    "for file in origin_pics:\n",
    "    if file in th_pics:\n",
    "        pass\n",
    "    else:\n",
    "        try:\n",
    "            cropimg(file)\n",
    "        except:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for file in origin_pics:\n",
    "    cropimg(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-08T13:50:04.117860Z",
     "start_time": "2021-12-08T13:50:04.105821Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['crop img.ipynb',\n",
       " 'output_20_0.png',\n",
       " 'bike.png',\n",
       " 'output_29_01.png',\n",
       " 'grid.html',\n",
       " 'output_11_0.png',\n",
       " '.ipynb_checkpoints',\n",
       " 'output_12_0.png']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "origin_pics =  os.listdir()\n",
    "origin_pics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-12-09T01:57:28.972708Z",
     "start_time": "2021-12-09T01:57:28.922495Z"
    }
   },
   "outputs": [],
   "source": [
    "def cropimg(file):\n",
    "    from PIL import Image\n",
    "    im = Image.open(file)\n",
    "    width,height = im.size\n",
    "    im = im.resize((int(width*0.3),int(height*0.3)))\n",
    "    width,height = im.size\n",
    "    a = int(min(width,(height/3*4))/4)\n",
    "    start1 = (width-a*4)/2\n",
    "    end1 = start1+a*4\n",
    "    start2 = (height-a*3)/2\n",
    "    end2 = start2+a*3\n",
    "    im = im.crop((int(start1),int(start2),int(end1),int(end2)))\n",
    "    im.save(file)\n",
    "origin_pics =  os.listdir()\n",
    "for file in origin_pics:\n",
    "    try:\n",
    "        cropimg(file)\n",
    "    except:\n",
    "        pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
